home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / time.c.z / time.c
Encoding:
C/C++ Source or Header  |  1998-10-28  |  15.1 KB  |  524 lines

  1. /* 
  2.    Unix SMB/Netbios implementation.
  3.    Version 1.9.
  4.    time handling functions
  5.    Copyright (C) Andrew Tridgell 1992-1998
  6.    
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 2 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    This program is distributed in the hope that it will be useful,
  13.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.    GNU General Public License for more details.
  16.    
  17.    You should have received a copy of the GNU General Public License
  18.    along with this program; if not, write to the Free Software
  19.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21.  
  22. #include "includes.h"
  23.  
  24. /*
  25.   This stuff was largely rewritten by Paul Eggert <eggert@twinsun.com>
  26.   in May 1996 
  27.   */
  28.  
  29.  
  30. int serverzone=0;
  31. int extra_time_offset = 0;
  32.  
  33. extern int DEBUGLEVEL;
  34.  
  35. #ifndef CHAR_BIT
  36. #define CHAR_BIT 8
  37. #endif
  38.  
  39. #ifndef TIME_T_MIN
  40. #define TIME_T_MIN ((time_t)0 < (time_t) -1 ? (time_t) 0 \
  41.             : ~ (time_t) 0 << (sizeof (time_t) * CHAR_BIT - 1))
  42. #endif
  43. #ifndef TIME_T_MAX
  44. #define TIME_T_MAX (~ (time_t) 0 - TIME_T_MIN)
  45. #endif
  46.  
  47.  
  48.  
  49. /*******************************************************************
  50. a gettimeofday wrapper
  51. ********************************************************************/
  52. void GetTimeOfDay(struct timeval *tval)
  53. {
  54. #ifdef GETTIMEOFDAY1
  55.   gettimeofday(tval);
  56. #else
  57.   gettimeofday(tval,NULL);
  58. #endif
  59. }
  60.  
  61. #define TM_YEAR_BASE 1900
  62.  
  63. /*******************************************************************
  64. yield the difference between *A and *B, in seconds, ignoring leap seconds
  65. ********************************************************************/
  66. static int tm_diff(struct tm *a, struct tm *b)
  67. {
  68.   int ay = a->tm_year + (TM_YEAR_BASE - 1);
  69.   int by = b->tm_year + (TM_YEAR_BASE - 1);
  70.   int intervening_leap_days =
  71.     (ay/4 - by/4) - (ay/100 - by/100) + (ay/400 - by/400);
  72.   int years = ay - by;
  73.   int days = 365*years + intervening_leap_days + (a->tm_yday - b->tm_yday);
  74.   int hours = 24*days + (a->tm_hour - b->tm_hour);
  75.   int minutes = 60*hours + (a->tm_min - b->tm_min);
  76.   int seconds = 60*minutes + (a->tm_sec - b->tm_sec);
  77.  
  78.   return seconds;
  79. }
  80.  
  81. /*******************************************************************
  82.   return the UTC offset in seconds west of UTC, or 0 if it cannot be determined
  83.   ******************************************************************/
  84. static int TimeZone(time_t t)
  85. {
  86.   struct tm *tm = gmtime(&t);
  87.   struct tm tm_utc;
  88.   if (!tm)
  89.     return 0;
  90.   tm_utc = *tm;
  91.   tm = localtime(&t);
  92.   if (!tm)
  93.     return 0;
  94.   return tm_diff(&tm_utc,tm);
  95. }
  96.  
  97.  
  98. /*******************************************************************
  99. init the time differences
  100. ********************************************************************/
  101. void TimeInit(void)
  102. {
  103.   serverzone = TimeZone(time(NULL));
  104.  
  105.   if ((serverzone % 60) != 0) {
  106.       DEBUG(1,("WARNING: Your timezone is not a multiple of 1 minute.\n"));
  107.   }
  108.  
  109.   DEBUG(4,("Serverzone is %d\n",serverzone));
  110. }
  111.  
  112.  
  113. /*******************************************************************
  114. return the same value as TimeZone, but it should be more efficient.
  115.  
  116. We keep a table of DST offsets to prevent calling localtime() on each 
  117. call of this function. This saves a LOT of time on many unixes.
  118.  
  119. Updated by Paul Eggert <eggert@twinsun.com>
  120. ********************************************************************/
  121. static int TimeZoneFaster(time_t t)
  122. {
  123.   static struct dst_table {time_t start,end; int zone;} *dst_table = NULL;
  124.   static int table_size = 0;
  125.   int i;
  126.   int zone = 0;
  127.  
  128.   if (t == 0) t = time(NULL);
  129.  
  130.   /* Tunis has a 8 day DST region, we need to be careful ... */
  131. #define MAX_DST_WIDTH (365*24*60*60)
  132. #define MAX_DST_SKIP (7*24*60*60)
  133.  
  134.   for (i=0;i<table_size;i++)
  135.     if (t >= dst_table[i].start && t <= dst_table[i].end) break;
  136.  
  137.   if (i<table_size) {
  138.     zone = dst_table[i].zone;
  139.   } else {
  140.     time_t low,high;
  141.  
  142.     zone = TimeZone(t);
  143.     dst_table = (struct dst_table *)Realloc(dst_table,
  144.                           sizeof(dst_table[0])*(i+1));
  145.     if (!dst_table) {
  146.       table_size = 0;
  147.     } else {
  148.       table_size++;
  149.  
  150.       dst_table[i].zone = zone; 
  151.       dst_table[i].start = dst_table[i].end = t;
  152.     
  153.       /* no entry will cover more than 6 months */
  154.       low = t - MAX_DST_WIDTH/2;
  155.       if (t < low)
  156.     low = TIME_T_MIN;
  157.       
  158.       high = t + MAX_DST_WIDTH/2;
  159.       if (high < t)
  160.     high = TIME_T_MAX;
  161.       
  162.       /* widen the new entry using two bisection searches */
  163.       while (low+60*60 < dst_table[i].start) {
  164.     if (dst_table[i].start - low > MAX_DST_SKIP*2)
  165.       t = dst_table[i].start - MAX_DST_SKIP;
  166.     else
  167.       t = low + (dst_table[i].start-low)/2;
  168.     if (TimeZone(t) == zone)
  169.       dst_table[i].start = t;
  170.     else
  171.       low = t;
  172.       }
  173.  
  174.       while (high-60*60 > dst_table[i].end) {
  175.     if (high - dst_table[i].end > MAX_DST_SKIP*2)
  176.       t = dst_table[i].end + MAX_DST_SKIP;
  177.     else
  178.       t = high - (high-dst_table[i].end)/2;
  179.     if (TimeZone(t) == zone)
  180.       dst_table[i].end = t;
  181.     else
  182.       high = t;
  183.       }
  184. #if 0
  185.       DEBUG(1,("Added DST entry from %s ",
  186.            asctime(localtime(&dst_table[i].start))));
  187.       DEBUG(1,("to %s (%d)\n",asctime(localtime(&dst_table[i].end)),
  188.            dst_table[i].zone));
  189. #endif
  190.     }
  191.   }
  192.   return zone;
  193. }
  194.  
  195. /****************************************************************************
  196.   return the UTC offset in seconds west of UTC, adjusted for extra time offset
  197.   **************************************************************************/
  198. int TimeDiff(time_t t)
  199. {
  200.   return TimeZoneFaster(t) + 60*extra_time_offset;
  201. }
  202.  
  203.  
  204. /****************************************************************************
  205.   return the UTC offset in seconds west of UTC, adjusted for extra time
  206.   offset, for a local time value.  If ut = lt + LocTimeDiff(lt), then
  207.   lt = ut - TimeDiff(ut), but the converse does not necessarily hold near
  208.   daylight savings transitions because some local times are ambiguous.
  209.   LocTimeDiff(t) equals TimeDiff(t) except near daylight savings transitions.
  210.   +**************************************************************************/
  211. static int LocTimeDiff(time_t lte)
  212. {
  213.   time_t lt = lte - 60*extra_time_offset;
  214.   int d = TimeZoneFaster(lt);
  215.   time_t t = lt + d;
  216.  
  217.   /* if overflow occurred, ignore all the adjustments so far */
  218.   if (((lte < lt) ^ (extra_time_offset < 0))  |  ((t < lt) ^ (d < 0)))
  219.     t = lte;
  220.  
  221.   /* now t should be close enough to the true UTC to yield the right answer */
  222.   return TimeDiff(t);
  223. }
  224.  
  225.  
  226. /****************************************************************************
  227. try to optimise the localtime call, it can be quite expensive on some machines
  228. ****************************************************************************/
  229. struct tm *LocalTime(time_t *t)
  230. {
  231.   time_t t2 = *t;
  232.  
  233.   t2 -= TimeDiff(t2);
  234.  
  235.   return(gmtime(&t2));
  236. }
  237.  
  238.  
  239. #define TIME_FIXUP_CONSTANT (369.0*365.25*24*60*60-(3.0*24*60*60+6.0*60*60))
  240.  
  241. /****************************************************************************
  242. interpret an 8 byte "filetime" structure to a time_t
  243. It's originally in "100ns units since jan 1st 1601"
  244.  
  245. It appears to be kludge-GMT (at least for file listings). This means
  246. its the GMT you get by taking a localtime and adding the
  247. serverzone. This is NOT the same as GMT in some cases. This routine
  248. converts this to real GMT.
  249. ****************************************************************************/
  250. time_t interpret_long_date(char *p)
  251. {
  252.   double d;
  253.   time_t ret;
  254.   uint32 tlow,thigh;
  255.   /* The next two lines are a fix needed for the
  256.      broken SCO compiler. JRA. */
  257.   time_t l_time_min = TIME_T_MIN;
  258.   time_t l_time_max = TIME_T_MAX;
  259.  
  260.   tlow = IVAL(p,0);
  261.   thigh = IVAL(p,4);
  262.  
  263.   if (thigh == 0) return(0);
  264.  
  265.   d = ((double)thigh)*4.0*(double)(1<<30);
  266.   d += (tlow&0xFFF00000);
  267.   d *= 1.0e-7;
  268.  
  269.   /* now adjust by 369 years to make the secs since 1970 */
  270.   d -= TIME_FIXUP_CONSTANT;
  271.  
  272.   if (!(l_time_min <= d && d <= l_time_max))
  273.     return(0);
  274.  
  275.   ret = (time_t)(d+0.5);
  276.  
  277.   /* this takes us from kludge-GMT to real GMT */
  278.   ret -= serverzone;
  279.   ret += LocTimeDiff(ret);
  280.  
  281.   return(ret);
  282. }
  283.  
  284.  
  285. /****************************************************************************
  286. put a 8 byte filetime from a time_t
  287. This takes real GMT as input and converts to kludge-GMT
  288. ****************************************************************************/
  289. void put_long_date(char *p,time_t t)
  290. {
  291.   uint32 tlow,thigh;
  292.   double d;
  293.  
  294.   if (t==0) {
  295.     SIVAL(p,0,0); SIVAL(p,4,0);
  296.     return;
  297.   }
  298.  
  299.   /* this converts GMT to kludge-GMT */
  300.   t -= LocTimeDiff(t) - serverzone; 
  301.  
  302.   d = (double) (t);
  303.  
  304.   d += TIME_FIXUP_CONSTANT;
  305.  
  306.   d *= 1.0e7;
  307.  
  308.   thigh = (uint32)(d * (1.0/(4.0*(double)(1<<30))));
  309.   tlow = (uint32)(d - ((double)thigh)*4.0*(double)(1<<30));
  310.  
  311.   SIVAL(p,0,tlow);
  312.   SIVAL(p,4,thigh);
  313. }
  314.  
  315.  
  316. /****************************************************************************
  317. check if it's a null mtime
  318. ****************************************************************************/
  319. BOOL null_mtime(time_t mtime)
  320. {
  321.   if (mtime == 0 || mtime == 0xFFFFFFFF || mtime == (time_t)-1)
  322.     return(True);
  323.   return(False);
  324. }
  325.  
  326. /*******************************************************************
  327.   create a 16 bit dos packed date
  328. ********************************************************************/
  329. static uint16 make_dos_date1(time_t unixdate,struct tm *t)
  330. {
  331.   uint16 ret=0;
  332.   ret = (((unsigned)(t->tm_mon+1)) >> 3) | ((t->tm_year-80) << 1);
  333.   ret = ((ret&0xFF)<<8) | (t->tm_mday | (((t->tm_mon+1) & 0x7) << 5));
  334.   return(ret);
  335. }
  336.  
  337. /*******************************************************************
  338.   create a 16 bit dos packed time
  339. ********************************************************************/
  340. static uint16 make_dos_time1(time_t unixdate,struct tm *t)
  341. {
  342.   uint16 ret=0;
  343.   ret = ((((unsigned)t->tm_min >> 3)&0x7) | (((unsigned)t->tm_hour) << 3));
  344.   ret = ((ret&0xFF)<<8) | ((t->tm_sec/2) | ((t->tm_min & 0x7) << 5));
  345.   return(ret);
  346. }
  347.  
  348. /*******************************************************************
  349.   create a 32 bit dos packed date/time from some parameters
  350.   This takes a GMT time and returns a packed localtime structure
  351. ********************************************************************/
  352. static uint32 make_dos_date(time_t unixdate)
  353. {
  354.   struct tm *t;
  355.   uint32 ret=0;
  356.  
  357.   t = LocalTime(&unixdate);
  358.   if (!t)
  359.     return 0xFFFFFFFF;
  360.  
  361.   ret = make_dos_date1(unixdate,t);
  362.   ret = ((ret&0xFFFF)<<16) | make_dos_time1(unixdate,t);
  363.  
  364.   return(ret);
  365. }
  366.  
  367. /*******************************************************************
  368. put a dos date into a buffer (time/date format)
  369. This takes GMT time and puts local time in the buffer
  370. ********************************************************************/
  371. void put_dos_date(char *buf,int offset,time_t unixdate)
  372. {
  373.   uint32 x = make_dos_date(unixdate);
  374.   SIVAL(buf,offset,x);
  375. }
  376.  
  377. /*******************************************************************
  378. put a dos date into a buffer (date/time format)
  379. This takes GMT time and puts local time in the buffer
  380. ********************************************************************/
  381. void put_dos_date2(char *buf,int offset,time_t unixdate)
  382. {
  383.   uint32 x = make_dos_date(unixdate);
  384.   x = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
  385.   SIVAL(buf,offset,x);
  386. }
  387.  
  388. /*******************************************************************
  389. put a dos 32 bit "unix like" date into a buffer. This routine takes
  390. GMT and converts it to LOCAL time before putting it (most SMBs assume
  391. localtime for this sort of date)
  392. ********************************************************************/
  393. void put_dos_date3(char *buf,int offset,time_t unixdate)
  394. {
  395.   if (!null_mtime(unixdate))
  396.     unixdate -= TimeDiff(unixdate);
  397.   SIVAL(buf,offset,unixdate);
  398. }
  399.  
  400. /*******************************************************************
  401.   interpret a 32 bit dos packed date/time to some parameters
  402. ********************************************************************/
  403. static void interpret_dos_date(uint32 date,int *year,int *month,int *day,int *hour,int *minute,int *second)
  404. {
  405.   uint32 p0,p1,p2,p3;
  406.  
  407.   p0=date&0xFF; p1=((date&0xFF00)>>8)&0xFF; 
  408.   p2=((date&0xFF0000)>>16)&0xFF; p3=((date&0xFF000000)>>24)&0xFF;
  409.  
  410.   *second = 2*(p0 & 0x1F);
  411.   *minute = ((p0>>5)&0xFF) + ((p1&0x7)<<3);
  412.   *hour = (p1>>3)&0xFF;
  413.   *day = (p2&0x1F);
  414.   *month = ((p2>>5)&0xFF) + ((p3&0x1)<<3) - 1;
  415.   *year = ((p3>>1)&0xFF) + 80;
  416. }
  417.  
  418. /*******************************************************************
  419.   create a unix date (int GMT) from a dos date (which is actually in
  420.   localtime)
  421. ********************************************************************/
  422. time_t make_unix_date(void *date_ptr)
  423. {
  424.   uint32 dos_date=0;
  425.   struct tm t;
  426.   time_t ret;
  427.  
  428.   dos_date = IVAL(date_ptr,0);
  429.  
  430.   if (dos_date == 0) return(0);
  431.   
  432.   interpret_dos_date(dos_date,&t.tm_year,&t.tm_mon,
  433.              &t.tm_mday,&t.tm_hour,&t.tm_min,&t.tm_sec);
  434.   t.tm_isdst = -1;
  435.   
  436.   /* mktime() also does the local to GMT time conversion for us */
  437.   ret = mktime(&t);
  438.  
  439.   return(ret);
  440. }
  441.  
  442. /*******************************************************************
  443. like make_unix_date() but the words are reversed
  444. ********************************************************************/
  445. time_t make_unix_date2(void *date_ptr)
  446. {
  447.   uint32 x,x2;
  448.  
  449.   x = IVAL(date_ptr,0);
  450.   x2 = ((x&0xFFFF)<<16) | ((x&0xFFFF0000)>>16);
  451.   SIVAL(&x,0,x2);
  452.  
  453.   return(make_unix_date((void *)&x));
  454. }
  455.  
  456. /*******************************************************************
  457.   create a unix GMT date from a dos date in 32 bit "unix like" format
  458.   these generally arrive as localtimes, with corresponding DST
  459.   ******************************************************************/
  460. time_t make_unix_date3(void *date_ptr)
  461. {
  462.   time_t t = IVAL(date_ptr,0);
  463.   if (!null_mtime(t))
  464.     t += LocTimeDiff(t);
  465.   return(t);
  466. }
  467.  
  468. /****************************************************************************
  469.   return the date and time as a string
  470. ****************************************************************************/
  471. char *timestring(void )
  472. {
  473.   static fstring TimeBuf;
  474.   time_t t = time(NULL);
  475.   struct tm *tm = LocalTime(&t);
  476.  
  477.   if (!tm)
  478.     slprintf(TimeBuf,sizeof(TimeBuf)-1,"%ld seconds since the Epoch",(long)t);
  479.   else
  480. #ifdef NO_STRFTIME
  481.   fstrcpy(TimeBuf, asctime(tm));
  482. #elif defined(CLIX) || defined(CONVEX)
  483.   strftime(TimeBuf,100,"%Y/%m/%d %I:%M:%S %p",tm);
  484. #elif defined(AMPM)
  485.   strftime(TimeBuf,100,"%Y/%m/%d %r",tm);
  486. #elif defined(TZ_TIME)
  487.   {
  488.     int zone = TimeDiff(t);
  489.     int absZoneMinutes = (zone<0 ? -zone : zone) / 60;
  490.     size_t len = strftime(TimeBuf,sizeof(TimeBuf)-6,"%Y/%m/%d %T",tm);
  491.     slprintf(TimeBuf+len, sizeof(TimeBuf) - len - 1, " %c%02d%02d",
  492.         zone<0?'+':'-',absZoneMinutes/60,absZoneMinutes%60);
  493.   }
  494. #else
  495.   strftime(TimeBuf,100,"%Y/%m/%d %T",tm);
  496. #endif
  497.   return(TimeBuf);
  498. }
  499.  
  500. /****************************************************************************
  501.   return the best approximation to a 'create time' under UNIX from a stat
  502.   structure.
  503. ****************************************************************************/
  504.  
  505. time_t get_create_time(struct stat *st,BOOL fake_dirs)
  506. {
  507.   time_t ret, ret1;
  508.  
  509.   if(S_ISDIR(st->st_mode) && fake_dirs)
  510.     return (time_t)315493200L;          /* 1/1/1980 */
  511.     
  512.   ret = MIN(st->st_ctime, st->st_mtime);
  513.   ret1 = MIN(ret, st->st_atime);
  514.  
  515.   if(ret1 != (time_t)0)
  516.     return ret1;
  517.  
  518.   /*
  519.    * One of ctime, mtime or atime was zero (probably atime).
  520.    * Just return MIN(ctime, mtime).
  521.    */
  522.   return ret;
  523. }
  524.